package com.hfxt.config;

import com.hfxt.utils.JwtUtil;
import com.hfxt.utils.Result;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

//如果用户从登录访问的话这个过滤器是不需执行的
//如果用户访问addContact findConcatct delContact 需要经过并解析token
@Component
public class FilterConfig extends ZuulFilter {

    @Value("${exclude.auth.url}")
    private String excludeAuthUrl;
    @Resource
    JwtUtil jwtUtil;

    @Override
    public String filterType() {//进路由前进行拦截
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 2;
    }

    @Override
    public boolean shouldFilter() {//该过滤器是否被启动
        RequestContext requestContext=RequestContext.getCurrentContext();
        ///api/login/userLogin
        String url=requestContext.getRequest().getRequestURI();
        List<String> excludeList= Arrays.asList(excludeAuthUrl.split(","));
        return !excludeList.contains(url);
    }

    @Override
    public Object run() throws ZuulException {//具体执行逻辑
        RequestContext requestContext=RequestContext.getCurrentContext();
        HttpServletRequest request=requestContext.getRequest();
        String token=request.getHeader("token");
        Claims claims=null;
        try {
             claims = jwtUtil.parseJWT(token);
             String roles=claims.get("roles").toString();
             if (roles.equals("admin")){
                 //验证通过
                 //放行
                 requestContext.setSendZuulResponse(true);
                 requestContext.setResponseStatusCode(200);
                 requestContext.addZuulResponseHeader("userid",roles);
//                 requestContext.getResponse().setContentType("text/html;charset=utf-8");
//                 requestContext.setResponseBody("验证通过");
                 responseBody(requestContext,200,"验证通过");
             }else {
                 requestContext.setSendZuulResponse(false);
                 requestContext.setResponseStatusCode(401);
                 requestContext.addZuulResponseHeader("userid",roles);
//                 requestContext.setResponseBody("权限不足");
                 responseBody(requestContext,401,"权限不足");
             }
        }catch (ExpiredJwtException e){
            requestContext.setSendZuulResponse(false);
            responseBody(requestContext,401,"token过期");
        }catch (Exception e){
            requestContext.setSendZuulResponse(false);
            responseBody(requestContext,500,"验证失败");
        }
        return null;
    }

    private void responseBody(RequestContext context,int code,String message){
        HttpServletRequest request=context.getRequest();
        Result result=new Result();
        result.setCode(code);
        result.setMsg(message);
        context.getResponse().setContentType("text/html;charset=utf-8");
        context.setResponseBody(result.toString());

    }

}
